home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / underwater / underwater.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  16.8 KB  |  634 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1997. */
  3.  
  4. /* This program is freely distributable without licensing fees and is
  5.    provided without guarantee or warrantee expressed or implied. This
  6.    program is -not- in the public domain. */
  7.  
  8. /* X compile line: cc -o underwater underwater.c texload.c dino.c -lglut -lGLU -lGL -lXmu -lXext -lX11 -lm */
  9.  
  10. /* This code compiles and works with any of 1) OpenGL 1.0 with no
  11.    texture extensions, 2) OpenGL 1.0 with texture extensions, or 3)
  12.    OpenGL 1.1. */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <math.h>
  18. #include <GL/glut.h>
  19.  
  20. #include "texload.h"
  21. #include "dino.h"
  22.  
  23. #if 0  /* For debugging different OpenGL versions. */
  24. #undef GL_VERSION_1_1
  25. #undef GL_EXT_texture_object
  26. #endif
  27.  
  28. /* Some <math.h> files do not define M_PI... */
  29. #ifndef M_PI
  30. #define M_PI 3.14159265358979323846
  31. #endif
  32.  
  33. /* Texture object compatibility. */
  34. #if defined(GL_VERSION_1_1)
  35. #define TEXTURE_OBJECT 1
  36. #elif defined(GL_EXT_texture_object)
  37. #define TEXTURE_OBJECT 1
  38. #define glBindTexture(A,B)     glBindTextureEXT(A,B)
  39. #define glGenTextures(A,B)     glGenTexturesEXT(A,B)
  40. #define glDeleteTextures(A,B)  glDeleteTexturesEXT(A,B)
  41. #ifndef GL_REPLACE
  42. #define GL_REPLACE GL_REPLACE_EXT
  43. #endif
  44. #else
  45. /* Define to nothing; but HaveTexObj will not be true in this case. */
  46. #define glBindTexture(A,B)
  47. #define glGenTextures(A,B)
  48. #define glDeleteTextures(A,B)
  49. #endif
  50.  
  51. #define NUM_PATTERNS 32
  52. #define FLOOR_FILE "floor.rgb"
  53.  
  54. enum {
  55.   PASS_NORMAL, PASS_CAUSTIC
  56. };
  57.  
  58. enum {
  59.   M_POSITIONAL, M_DIRECTIONAL, M_GREENISH_LIGHT, M_WHITE_LIGHT,
  60.   M_NO_CAUSTICS, M_WITH_CAUSTICS, M_SWITCH_MODEL,
  61.   M_INCREASE_RIPPLE_SIZE, M_DECREASE_RIPPLE_SIZE
  62. };
  63.   
  64. enum {
  65.   MODEL_SPHERE, MODEL_CUBE, MODEL_DINO
  66. };
  67.  
  68. static GLboolean HaveTexObj = GL_FALSE;
  69. static int object = MODEL_SPHERE;
  70. static int reportSpeed = 0;
  71. static int dinoDisplayList;
  72. static GLfloat causticScale = 1.0;
  73. static int fullscreen = 0;
  74.  
  75. static GLfloat lightPosition[4];
  76. /* XXX Diffuse light color component > 1.0 to brighten caustics. */
  77. static GLfloat lightDiffuseColor[] = {1.0, 1.5, 1.0, 1.0};  /* XXX Green = 1.5 */
  78. static GLfloat defaultDiffuseMaterial[] = {0.8, 0.8, 0.8, 1.0};
  79.  
  80. static int directionalLight = 1;
  81. static int showCaustics = 1, causticMotion = 1;
  82. static int useMipmaps = 1;
  83. static int currentCaustic = 0;
  84. static int causticIncrement = 1;
  85.  
  86. static float lightAngle = 0.0, lightHeight = 20;
  87. static GLfloat angle = -150;   /* in degrees */
  88. static GLfloat angle2 = 30;   /* in degrees */
  89.  
  90. static int moving = 0, startx, starty;
  91. static int lightMoving = 0, lightStartX, lightStartY;
  92.  
  93. static GLfloat floorVertices[4][3] = {
  94.   { -20.0, 0.0, 20.0 },
  95.   { 20.0, 0.0, 20.0 },
  96.   { 20.0, 0.0, -20.0 },
  97.   { -20.0, 0.0, -20.0 },
  98. };
  99.  
  100. void
  101. drawLightLocation(void)
  102. {
  103.   glPushMatrix();
  104.   glDisable(GL_LIGHTING);
  105.   glDisable(GL_TEXTURE_2D);
  106.   glColor3f(1.0, 1.0, 0.0);
  107.   if (directionalLight) {
  108.     /* Draw an arrowhead. */
  109.     glDisable(GL_CULL_FACE);
  110.     glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);
  111.     glRotatef(lightAngle * -180.0 / M_PI, 0, 1, 0);
  112.     glRotatef(atan(lightHeight/12) * 180.0 / M_PI, 0, 0, 1);
  113.     glBegin(GL_TRIANGLE_FAN);
  114.       glVertex3f(0, 0, 0);
  115.       glVertex3f(2, 1, 1);
  116.       glVertex3f(2, -1, 1);
  117.       glVertex3f(2, -1, -1);
  118.       glVertex3f(2, 1, -1);
  119.       glVertex3f(2, 1, 1);
  120.     glEnd();
  121.     /* Draw a white line from light direction. */
  122.     glColor3f(1.0, 1.0, 1.0);
  123.     glBegin(GL_LINES);
  124.       glVertex3f(0, 0, 0);
  125.       glVertex3f(5, 0, 0);
  126.     glEnd();
  127.     glEnable(GL_CULL_FACE);
  128.   } else {
  129.     /* Draw a yellow ball at the light source. */
  130.     glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);
  131.     glutSolidSphere(1.0, 5, 5);
  132.   }
  133.   glEnable(GL_TEXTURE_2D);
  134.   glEnable(GL_LIGHTING);
  135.   glPopMatrix();
  136. }
  137.  
  138. /* Draw a floor (possibly textured). */
  139. static void
  140. drawFloor(int pass)
  141. {
  142.   if (pass == PASS_NORMAL) {
  143.     if (HaveTexObj)
  144.       glBindTexture(GL_TEXTURE_2D, 100);
  145.     else
  146.       glCallList(100);
  147.   }
  148.  
  149.   /* The glTexCoord2f calls get ignored when in texture generation
  150.      mode (ie, when modulating in caustics). */
  151.  
  152.   glBegin(GL_QUADS);
  153.     glNormal3f(0.0, 1.0, 0.0);
  154.     glTexCoord2f(0.0, 0.0);
  155.     glVertex3fv(floorVertices[0]);
  156.     glTexCoord2f(0.0, 2.0);
  157.     glVertex3fv(floorVertices[1]);
  158.     glTexCoord2f(2.0, 2.0);
  159.     glVertex3fv(floorVertices[2]);
  160.     glTexCoord2f(2.0, 0.0);
  161.     glVertex3fv(floorVertices[3]);
  162.   glEnd();
  163. }
  164.  
  165. void
  166. drawObject(int pass)
  167. {
  168.   if (pass == PASS_NORMAL) {
  169.     /* The objects are not textured (they could be if someone
  170.        wanted them to be) so disable texture in the normal pass.  In
  171.        the caustic pass, we want to avoid disabling texture though. */
  172.     glDisable(GL_TEXTURE_2D);
  173.   }
  174.  
  175.   glPushMatrix();
  176.  
  177.     /* If object is dino, put feet on the floor. */
  178.     glTranslatef(0.0, object == MODEL_DINO ? 8.0 : 12.0, 0.0);
  179.  
  180.     switch (object) {
  181.     case MODEL_SPHERE:
  182.       glutSolidSphere(6.0, 12, 12);
  183.       break;
  184.     case MODEL_CUBE:
  185.       glutSolidCube(7.0);
  186.       break;
  187.     case MODEL_DINO:
  188.       glCallList(dinoDisplayList);
  189.       glMaterialfv(GL_FRONT, GL_DIFFUSE, defaultDiffuseMaterial);
  190.       break;
  191.     }
  192.   glPopMatrix();
  193.  
  194.   if (pass == PASS_NORMAL) {
  195.     glEnable(GL_TEXTURE_2D);
  196.   }
  197. }
  198.  
  199. void
  200. drawScene(int pass)
  201. {
  202.   /* The 0.03 in the Y column is just to shift the texture coordinates
  203.      a little based on Y (depth in the water) so that vertical faces
  204.      (like on the cube) do not get totally vertical caustics. */
  205.   GLfloat sPlane[4] = { 0.05, 0.03, 0.0, 0.0 };
  206.   GLfloat tPlane[4] = { 0.0, 0.03, 0.05, 0.0 };
  207.  
  208.   /* The causticScale determines how large the caustic "ripples" will
  209.      be.  See the "Increate/Decrease ripple size" menu options. */
  210.  
  211.   sPlane[0] = 0.05 * causticScale;
  212.   sPlane[1] = 0.03 * causticScale;
  213.  
  214.   tPlane[1] = 0.03 * causticScale;
  215.   tPlane[2] = 0.05 * causticScale;
  216.  
  217.   if (pass == PASS_CAUSTIC) {
  218.     /* Set current color to "white" and disable lighting
  219.        to emulate OpenGL 1.1's GL_REPLACE texture environment. */
  220.     glColor3f(1.0, 1.0, 1.0);
  221.     glDisable(GL_LIGHTING);
  222.  
  223.     /* Generate the S & T coordinates for the caustic textures
  224.        from the object coordinates. */
  225.  
  226.     glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  227.     glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  228.     glTexGenfv(GL_S, GL_OBJECT_PLANE, sPlane);
  229.     glTexGenfv(GL_T, GL_OBJECT_PLANE, tPlane);
  230.     glEnable(GL_TEXTURE_GEN_S);
  231.     glEnable(GL_TEXTURE_GEN_T);
  232.  
  233.     if (HaveTexObj) {
  234.       glBindTexture(GL_TEXTURE_2D, currentCaustic+1);
  235.     } else {
  236.       glCallList(currentCaustic+101);
  237.     }
  238.   }
  239.  
  240.   drawFloor(pass);
  241.   drawObject(pass);
  242.  
  243.   if (pass == PASS_CAUSTIC) {
  244.     glEnable(GL_LIGHTING);
  245.     glDisable(GL_TEXTURE_GEN_S);
  246.     glDisable(GL_TEXTURE_GEN_T);
  247.   }
  248. }
  249.  
  250. void
  251. display(void)
  252. {
  253.   int startTime, endTime;
  254.  
  255.   /* Simplistic benchmarking.  Be careful about results. */
  256.   if (reportSpeed) {
  257.     startTime = glutGet(GLUT_ELAPSED_TIME);
  258.   }
  259.  
  260.   /* Clear depth and color buffer. */
  261.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  262.  
  263.   /* Reposition the light source. */
  264.   lightPosition[0] = 12*cos(lightAngle);
  265.   lightPosition[1] = lightHeight;
  266.   lightPosition[2] = 12*sin(lightAngle);
  267.   if (directionalLight) {
  268.     lightPosition[3] = 0.0;
  269.   } else {
  270.     lightPosition[3] = 1.0;
  271.   }
  272.  
  273.   glPushMatrix();
  274.     /* Perform scene rotations based on user mouse input. */
  275.     glRotatef(angle2, 1.0, 0.0, 0.0);
  276.     glRotatef(angle, 0.0, 1.0, 0.0);
  277.     
  278.     /* Position the light again, after viewing rotation. */
  279.     glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  280.  
  281.     /* Draw the light location. */
  282.     drawLightLocation();
  283.  
  284.     /* Normal pass rendering the scene (caustics get added
  285.        after this pass). */
  286.     drawScene(PASS_NORMAL);
  287.  
  288.     if (showCaustics) {
  289.       /* Disable depth buffer update and exactly match depth
  290.      buffer values for slightly faster rendering. */
  291.       glDepthMask(GL_FALSE);
  292.       glDepthFunc(GL_EQUAL);
  293.  
  294.       /* Multiply the source color (from the caustic luminance
  295.      texture) with the previous color from the normal pass.  The
  296.      caustics are modulated into the scene. */
  297.       glBlendFunc(GL_ZERO, GL_SRC_COLOR);
  298.       glEnable(GL_BLEND);
  299.  
  300.       drawScene(PASS_CAUSTIC);
  301.  
  302.       /* Restore fragment operations to normal. */
  303.       glDepthMask(GL_TRUE);
  304.       glDepthFunc(GL_LESS);
  305.       glDisable(GL_BLEND);
  306.     }
  307.   glPopMatrix();
  308.  
  309.   glutSwapBuffers();
  310.  
  311.   if (reportSpeed) {
  312.     glFinish();
  313.     endTime = glutGet(GLUT_ELAPSED_TIME);
  314.     printf("Speed %.3g frames/sec (%d ms)\n", 1000.0/(endTime-startTime), endTime-startTime);
  315.     fflush(stdout);
  316.   }
  317. }
  318.  
  319. void reshape(int w, int h)
  320. {
  321.    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  322.    glMatrixMode(GL_PROJECTION);
  323.    glLoadIdentity();
  324.    gluPerspective(40.0, /* field of view in degree */
  325.      (GLfloat) w/(GLfloat) h, /* aspect ratio */
  326.      20.0, /* Z near */
  327.      100.0); /* Z far */
  328.    glMatrixMode(GL_MODELVIEW);
  329. }
  330.  
  331. void
  332. idle(void)
  333. {
  334.   /* Advance the caustic pattern. */
  335.   currentCaustic = (currentCaustic + causticIncrement) % NUM_PATTERNS;
  336.   glutPostRedisplay();
  337. }
  338.  
  339. void
  340. updateIdleFunc(void)
  341. {
  342.   /* Must be both displaying the caustic patterns and have the
  343.      caustics in rippling motion to need an idle callback. */
  344.   if (showCaustics && causticMotion) {
  345.     glutIdleFunc(idle);
  346.   } else {
  347.     glutIdleFunc(NULL);
  348.   }
  349. }
  350.  
  351. void 
  352. visible(int vis)
  353. {
  354.   /* Stop the animation when the window is not visible. */
  355.   if (vis == GLUT_VISIBLE)
  356.     updateIdleFunc();
  357.   else
  358.     glutIdleFunc(NULL);
  359. }
  360.  
  361. /* ARGSUSED2 */
  362. static void
  363. mouse(int button, int state, int x, int y)
  364. {
  365.   /* Rotate the scene with the left mouse button. */
  366.   if (button == GLUT_LEFT_BUTTON) {
  367.     if (state == GLUT_DOWN) {
  368.       moving = 1;
  369.       startx = x;
  370.       starty = y;
  371.     }
  372.     if (state == GLUT_UP) {
  373.       moving = 0;
  374.     }
  375.   }
  376.   /* Rotate the light position with the middle mouse button. */
  377.   if (button == GLUT_MIDDLE_BUTTON) {
  378.     if (state == GLUT_DOWN) {
  379.       lightMoving = 1;
  380.       lightStartX = x;
  381.       lightStartY = y;
  382.     }
  383.     if (state == GLUT_UP) {
  384.       lightMoving = 0;
  385.     }
  386.   }
  387. }
  388.  
  389. /* ARGSUSED1 */
  390. static void
  391. motion(int x, int y)
  392. {
  393.   if (moving) {
  394.     angle = angle + (x - startx);
  395.     angle2 = angle2 + (y - starty);
  396.     startx = x;
  397.     starty = y;
  398.     glutPostRedisplay();
  399.   }
  400.   if (lightMoving) {
  401.     lightAngle += (x - lightStartX)/40.0;
  402.     lightHeight += (lightStartY - y)/20.0;
  403.     lightStartX = x;
  404.     lightStartY = y;
  405.     glutPostRedisplay();
  406.   }
  407. }
  408.  
  409. /* ARGSUSED1 */
  410. static void
  411. keyboard(unsigned char c, int x, int y)
  412. {
  413.   switch (c) {
  414.   case 27:  /* Escape quits. */
  415.     exit(0);
  416.     break;
  417.   case 'R':  /* Simplistic benchmarking. */
  418.   case 'r':
  419.     reportSpeed = !reportSpeed;
  420.     break;
  421.   case ' ':  /* Spacebar toggles caustic rippling motion. */
  422.     causticMotion = !causticMotion;
  423.     updateIdleFunc();
  424.     break;
  425.   }
  426. }
  427.  
  428. void
  429. menuSelect(int value)
  430. {
  431.   switch (value) {
  432.   case M_POSITIONAL:
  433.     directionalLight = 0;
  434.     break;
  435.   case M_DIRECTIONAL:
  436.     directionalLight = 1;
  437.     break;
  438.   case M_GREENISH_LIGHT:
  439.     lightDiffuseColor[0] = 1.0;
  440.     lightDiffuseColor[1] = 1.5;  /* XXX Green = 1.5 */
  441.     lightDiffuseColor[2] = 1.0;
  442.     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuseColor);
  443.     break;
  444.   case M_WHITE_LIGHT:
  445.     lightDiffuseColor[0] = 1.5;  /* XXX Red = 1.5 */
  446.     lightDiffuseColor[1] = 1.5;  /* XXX Green = 1.5 */
  447.     lightDiffuseColor[2] = 1.5;  /* XXX Blue = 1.5 */
  448.     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuseColor);
  449.     break;
  450.   case M_WITH_CAUSTICS:
  451.     showCaustics = 1;
  452.     updateIdleFunc();
  453.     break;
  454.   case M_NO_CAUSTICS:
  455.     showCaustics = 0;
  456.     updateIdleFunc();
  457.     break;
  458.   case M_SWITCH_MODEL:
  459.     object = (object + 1) % 3;
  460.     break;
  461.   case M_INCREASE_RIPPLE_SIZE:
  462.     causticScale /= 1.5;
  463.     break;
  464.   case M_DECREASE_RIPPLE_SIZE:
  465.     causticScale *= 1.5;
  466.     break;
  467.   }
  468.   glutPostRedisplay();
  469. }
  470.  
  471. int
  472. main(int argc, char **argv)
  473. {
  474.   int width, height;
  475.   int i;
  476.   GLubyte *imageData;
  477.  
  478.   glutInit(&argc, argv);
  479.   for (i=1; i<argc; i++) {
  480.     if (!strcmp("-lesstex", argv[i])) {
  481.       /* Only use 16 caustic textures.  Saves texture memory and works
  482.      better on slow machines. */
  483.       causticIncrement = 2;
  484.     } else if (!strcmp("-evenlesstex", argv[i])) {
  485.       /* Only use 8 caustic textures.  Saves even more texture memory for
  486.      slow machines.  Temporal rippling suffers. */
  487.       causticIncrement = 4;
  488.     } else if (!strcmp("-nomipmap", argv[i])) {
  489.       /* Don't use linear mipmap linear texture filtering; instead
  490.      use linear filtering. */
  491.       useMipmaps = 0;
  492.     } else if (!strcmp("-fullscreen", argv[i])) {
  493.       fullscreen = 1;
  494.     } else {
  495.       fprintf(stderr, "usage: caustics [-lesstex]\n");
  496.       fprintf(stderr, "       -lesstex uses half the caustic textures.\n");
  497.       fprintf(stderr, "       -evenlesstex uses one fourth of the caustic textures.\n");
  498.       exit(1);
  499.     }
  500.   }
  501.  
  502.   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  503.   if (fullscreen) {
  504.     glutGameModeString("800x600:16@60");
  505.     glutEnterGameMode();
  506.   } else {
  507.     glutCreateWindow("underwater");
  508.   }
  509.  
  510.   /* Check that renderer has the GL_EXT_texture_object
  511.      extension or supports OpenGL 1.1 */
  512. #ifdef TEXTURE_OBJECT
  513.   {
  514.     char *version = (char *) glGetString(GL_VERSION);
  515.     if (glutExtensionSupported("GL_EXT_texture_object")
  516.       || strncmp(version, "1.1", 3) == 0) {
  517.       HaveTexObj = GL_TRUE;
  518.     }
  519.   }
  520. #endif
  521.  
  522.   glEnable(GL_TEXTURE_2D);
  523. #ifdef TEXTURE_OBJECT  /* Replace texture environment not in OpenGL 1.0. */
  524.   if (HaveTexObj)
  525.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  526. #endif
  527.  
  528.   /* Load the caustic ripple textures. */
  529.   printf("loading caustics:");
  530.   for (i=0; i<NUM_PATTERNS; i += causticIncrement) {
  531.     char filename[80];
  532.  
  533.     sprintf(filename, "caust%02d.bw", i);
  534.     printf(" %d", i);
  535.     fflush(stdout);
  536.     imageData = read_alpha_texture(filename, &width, &height);
  537.     if (imageData == NULL) {
  538.       fprintf(stderr, "\n%s: could not load image file\n", filename);
  539.       exit(1);
  540.     }
  541.     if (HaveTexObj)
  542.       glBindTexture(GL_TEXTURE_2D, i+1);
  543.     else
  544.       glNewList(i+101, GL_COMPILE);
  545.     if (useMipmaps) {
  546.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  547.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  548.       gluBuild2DMipmaps(GL_TEXTURE_2D, GL_LUMINANCE, width, height,
  549.         GL_LUMINANCE, GL_UNSIGNED_BYTE, imageData);
  550.     } else {
  551.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  552.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  553.       glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, height, width, 0,
  554.         GL_LUMINANCE, GL_UNSIGNED_BYTE, imageData);
  555.     }
  556.     free(imageData);
  557.     if (!HaveTexObj)
  558.       glEndList();
  559.   }
  560.   printf(".\n");
  561.  
  562.   /* Load an RGB file for the floor texture. */
  563.   printf("loading RGB textures: floor");
  564.   fflush(stdout);
  565.   imageData = read_rgb_texture(FLOOR_FILE, &width, &height);
  566.   if (imageData == NULL) {
  567.     fprintf(stderr, "%s: could not load image file\n", FLOOR_FILE);
  568.     exit(1);
  569.   }
  570.   printf(".\n");
  571.  
  572.   if (HaveTexObj)
  573.     glBindTexture(GL_TEXTURE_2D, 100);
  574.   else
  575.     glNewList(100, GL_COMPILE);
  576.   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  577.   if (useMipmaps) {
  578.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  579.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  580.     gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB,
  581.       GL_UNSIGNED_BYTE, imageData);
  582.   } else {
  583.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  584.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  585.     glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0,
  586.       GL_RGB, GL_UNSIGNED_BYTE, imageData);
  587.   }
  588.   free(imageData);
  589.   if (!HaveTexObj)
  590.     glEndList();
  591.  
  592.   glMatrixMode(GL_MODELVIEW);
  593.   gluLookAt(0.0, 8.0, 60.0,  /* eye is at (0,8,60) */
  594.     0.0, 8.0, 0.0,      /* center is at (0,8,0) */
  595.     0.0, 1.0, 0.);      /* up is in postivie Y direction */
  596.  
  597.   /* Setup initial OpenGL rendering state. */
  598.   glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuseColor);
  599.   glEnable(GL_LIGHT0);
  600.   glEnable(GL_LIGHTING);
  601.   glEnable(GL_DEPTH_TEST);
  602.   glEnable(GL_CULL_FACE);
  603.  
  604.   /* Register assorted GLUT callback routines. */
  605.   glutDisplayFunc(display);
  606.   glutReshapeFunc(reshape);
  607.   glutVisibilityFunc(visible);
  608.   glutMouseFunc(mouse);
  609.   glutMotionFunc(motion);
  610.   glutKeyboardFunc(keyboard);
  611.  
  612.   /* Create a pop-up menu. */
  613.   if (!fullscreen) {
  614.     glutCreateMenu(menuSelect);
  615.     glutAddMenuEntry("Positional light", M_POSITIONAL);
  616.     glutAddMenuEntry("Directional light", M_DIRECTIONAL);
  617.     glutAddMenuEntry("Greenish light", M_GREENISH_LIGHT);
  618.     glutAddMenuEntry("White light", M_WHITE_LIGHT);
  619.     glutAddMenuEntry("With caustics", M_WITH_CAUSTICS);
  620.     glutAddMenuEntry("No caustics", M_NO_CAUSTICS);
  621.     glutAddMenuEntry("Switch model", M_SWITCH_MODEL);
  622.     glutAddMenuEntry("Increase ripple size", M_INCREASE_RIPPLE_SIZE);
  623.     glutAddMenuEntry("Decrease ripple size", M_DECREASE_RIPPLE_SIZE);
  624.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  625.   }
  626.  
  627.   /* For rendering the MODEL_DINO object. */
  628.   dinoDisplayList = makeDinosaur();
  629.  
  630.   /* Enter GLUT's main loop; callback dispatching begins. */
  631.   glutMainLoop();
  632.   return 0;             /* ANSI C requires main to return int. */
  633. }
  634.